/**
 * @description 给数字添加分隔符
 * @param num 需要添加分隔符数字
 * @param counts 分隔位数，默认每3个数字添加一个分隔符
 * @param mark 分隔字符，默认英文逗号
 * @returns string
 */
export function formatNum(
  num: number | string,
  counts = 3,
  mark = ","
): string {
  if (!num) {
    return "0";
  }
  // console.log("formatNum num:", num);
  const numArr: string[] = num.toString().split(".");
  const tempAry = numArr[0].split("").reverse();
  const res = [];
  for (let i = 0, len = tempAry.length; i < len; i++) {
    if (i % counts === 0 && i !== 0) {
      res.push(mark);
    }
    res.push(tempAry[i]);
  }
  res.reverse();
  let resStr = "";
  if (numArr[1]) {
    resStr = res.join("").concat("." + numArr[1]);
  } else {
    resStr = res.join("");
  }

  return resStr;
}

export function convertToPrecentNum(
  currentNum: number,
  totalNum: number
): number {
  if (totalNum === 0) {
    return 0;
  }
  // console.log("currentNum / totalNum:", currentNum / totalNum);
  return Math.floor((currentNum / totalNum) * 1000) / 10;
}

export function deepCopy<T>(data: T): T {
  if (typeof data !== "object" || data === null) {
    throw new TypeError("传入参数不是对象");
  }
  const newData = {} as T;
  const dataKeys = Object.keys(data);
  dataKeys.forEach(value => {
    const currentDataValue = data[value];
    // 基本数据类型的值和函数直接赋值拷贝
    if (typeof currentDataValue !== "object" || currentDataValue === null) {
      // console.log("currentDataValue:", currentDataValue);
      newData[value] = currentDataValue;
    } else if (Array.isArray(currentDataValue)) {
      // 实现数组的深拷贝
      newData[value] = [...currentDataValue];
    } else if (currentDataValue instanceof Set) {
      // 实现set数据的深拷贝
      newData[value] = new Set([...currentDataValue]);
    } else if (currentDataValue instanceof Map) {
      // 实现map数据的深拷贝
      newData[value] = new Map([...currentDataValue]);
    } else {
      // 普通对象则递归赋值
      newData[value] = deepCopy<typeof currentDataValue>(currentDataValue);
    }
  });
  return newData;
}

export function formatDate(date: Date, fmt = "yyyy-MM-dd"): string {
  if (typeof date == "string") {
    return date;
  }

  if (!date || date == null) return null;
  const o = {
    "M+": date.getMonth() + 1, // 月份
    "d+": date.getDate(), // 日
    "h+": date.getHours(), // 小时
    "m+": date.getMinutes(), // 分
    "s+": date.getSeconds(), // 秒
    "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
    S: date.getMilliseconds() // 毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  for (const k in o) {
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
  }
  return fmt;
}

function getLastWeekDays() {
  const days = [];
  const oneDayTime = 86400000;
  const today = new Date();
  const todayStr = formatDate(today, "yyyy-MM-dd");
  const today2 = new Date(todayStr);

  for (let i = 7; i >= 1; i--) {
    const timeNum = today2.getTime() - i * oneDayTime;
    days.push(new Date(timeNum));
  }
  const fDays = days.map(item => {
    return formatDate(item, "yyyy-MM-dd");
  });
  return fDays;
}
export const lastWeeks = getLastWeekDays();

export function getPlainText(htmlString) {
  const tempDiv = document.createElement("div");
  tempDiv.innerHTML = htmlString;
  return tempDiv.textContent || tempDiv.innerText || "";
}

export function formatHtmlDes(htmlStr, len = 20) {
  let str = getPlainText(htmlStr);
  if (str.length > len) {
    str = str.slice(0, 20) + "...";
  }
  return str;
}
